﻿using CefSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DouYin
{
    public partial class Spider : Form
    {
        private readonly string url;

        public Spider(string url)
        {
            InitializeComponent();
            this.url = url;
        }
        /// <summary>
        /// 获取html的内容
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetHtmlContent()
        {
            //if (browser1.BrowserSettings==null)
            //{
            //    browser1.BrowserSettings = new BrowserSettings();
            //    browser1.BrowserSettings.ImageLoading = CefSharp.CefState.Disabled;
            //}


            //browser1.BrowserSettings.ImageShrinkStandaloneToFit = CefSharp.CefState.Disabled;
            //browser1.BrowserSettings.Javascript = CefSharp.CefState.Disabled;
            //browser1.BrowserSettings.JavascriptAccessClipboard = CefSharp.CefState.Disabled;
            //browser1.BrowserSettings.Plugins = CefSharp.CefState.Disabled;
            // browser1.BrowserSettings.WebGl = CefSharp.CefState.Disabled;
            this.chromiumWebBrowser1.Load(url);
            var result = await this.chromiumWebBrowser1.WaitForInitialLoadAsync();

            var len = new Random().Next(1, 10);  
            for (int i = 0; i < len; i++)
            {  
                Thread.Sleep(500);
                await this.chromiumWebBrowser1.EvaluateScriptAsync($"window.scrollTo(0, {i*600})");
            }

            if (result.Success)
            {
                Thread.Sleep(1000);
                return await this.chromiumWebBrowser1.GetSourceAsync();
            }
            return "";
        }
    }
}
